//
//  BaseNavigationController.m
//  mike2.0
//
//  Created by mike on 15/9/7.
//  Copyright (c) 2015年 flychi. All rights reserved.
//

#import "BaseNavigationController.h"
#import "BaseViewController.h"
#import "UINavigationItem+CustomBackButton.h"

@interface BaseNavigationController ()

@end

@implementation BaseNavigationController

#pragma mark - Life cycle

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.viewControllers.count) {
        viewController.hidesBottomBarWhenPushed = YES;
        
        //统一定制除了根控制器外的所有视图的导航栏控制器的左边按钮
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"back"] forState:UIControlStateHighlighted];
        button.frame = CGRectMake(0, 22, 70, 26);
        //        button.backgroundColor = [UIColor redColor];
        // 让按钮内部的所有内容左对齐
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        // 让按钮的内容往右边偏移5
        button.contentEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
        [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        //自定义了左边的按钮导致不能用左滑返回，清楚代理就能左滑
        self.interactivePopGestureRecognizer.delegate = nil;
    }
    //7.0以后需要关闭该功能,ScrollView才不会出现contentsize设置的很小也会出现滚动的问题
    if ([[UIDevice currentDevice] systemVersion].floatValue >= 7.0){
        viewController.automaticallyAdjustsScrollViewInsets = NO;
    }
    [super pushViewController:viewController animated:animated];
}

- (void)back{
    
    [self popViewControllerAnimated:YES];
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated{
    if ([[self.viewControllers lastObject] isKindOfClass:[BaseViewController class]]) {
        BaseViewController *viewController=[self.viewControllers lastObject];
        [viewController willPopViewController];
    }
    return [super popViewControllerAnimated:animated];
}

#pragma mark - View Rotation

- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

@end
